package com.example.kuaizu.service.imp;

import com.example.kuaizu.mapper.ResumeFunctionMapper;
import com.example.kuaizu.pojo.ov.ResumeVo;
import com.example.kuaizu.pojo.pram.CreateResumePram;
import com.example.kuaizu.pojo.pram.QueryResumePram;
import com.example.kuaizu.pojo.pram.UpdateResumePram;
import com.example.kuaizu.service.InResumeFunctionService;
import com.example.kuaizu.utils.UserUtils;
import kuaizu.common.exception.MassageException;
import kuaizu.common.tablepojo.Resume;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author: wangmx15
 * @date: 2025/10/6
 **/
@Service
@RequiredArgsConstructor
public class ResumeFunctionService implements InResumeFunctionService {
    private final ResumeFunctionMapper resumeFunctionMapper;

    /**
     * 创建简历
     *
     * @param pram
     */
    @Transactional
    @Override
    public void createResume(CreateResumePram pram) {
        Date now = new Date();
        Resume resume = new Resume();
        BeanUtils.copyProperties(pram, resume);
        resume.setCreateTime(now);
        resume.setUpdateTime(now);
        resume.setOpenid(UserUtils.getOpenId());
        //判断结束时间是否小于开始时间 并且不能处于未来
        if (StringUtils.hasText(pram.getEndTime()) && pram.getEndTime().compareTo(pram.getJoinTime()) < 0) {
            throw new MassageException("结束时间不能早于开始时间");
        }
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateStr = date.format(formatter);
        if (dateStr.compareTo(pram.getJoinTime()) < 0) {
            throw new MassageException("开始时间不能处于未来");
        }
        resumeFunctionMapper.insertResume(resume);
    }

    @Transactional
    @Override
    public List<ResumeVo> queryMyResumeAll() {
        String openId = UserUtils.getOpenId();
        List<ResumeVo> resumes = resumeFunctionMapper.queryMyResumeAll(openId);
        if (CollectionUtils.isEmpty(resumes)) {
            throw new MassageException("暂时还没有项目简历哈");
        }
        for (ResumeVo resume : resumes) {
            if (!StringUtils.hasText(resume.getEndTime())) {
                resume.setEndTime("至今");
            }
        }
        return resumes;
    }

    @Transactional
    @Override
    public ResumeVo queryResume(QueryResumePram pram) {
        return resumeFunctionMapper.selectResumeById(pram.getId());
    }

    @Transactional
    @Override
    public void delectResume(Integer id) {
        if (1 != resumeFunctionMapper.delectResumeByIdAndOpenId(id, UserUtils.getOpenId())) {
            throw new MassageException("删除失败");
        }
    }

    @Transactional
    @Override
    public void updateResume(UpdateResumePram pram) {
        ResumeVo resumeVo = resumeFunctionMapper.selectResumeById(pram.getId());
        if (ObjectUtils.isEmpty(resumeVo)) {
            throw new MassageException("没有此简历");
        }
        if (!StringUtils.hasText(pram.getJoinTime()) || !StringUtils.hasText(pram.getEndTime())) {
            throw new MassageException("开始和结束时间必须成对出现");
        } else if (StringUtils.hasText(pram.getJoinTime()) && StringUtils.hasText(pram.getEndTime())) {
            if (StringUtils.hasText(pram.getEndTime()) && pram.getEndTime().compareTo(pram.getJoinTime()) < 0) {
                throw new MassageException("结束时间不能早于开始时间");
            }
            LocalDate date = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String dateStr = date.format(formatter);
            if (dateStr.compareTo(pram.getJoinTime()) < 0) {
                throw new MassageException("开始时间不能处于未来");
            }
        }
        Date now = new Date();
        Resume resume = new Resume();
        BeanUtils.copyProperties(pram, resume);
        resume.setOpenid(UserUtils.getOpenId());
        resume.setUpdateTime(now);
        if (1 != resumeFunctionMapper.updateResumeById(resume)) {
            throw new MassageException("更新失败");
        }
    }
}
